package cn.zwx.activiti.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.zwx.activiti.example.model.req.InstancesByUserReq;
import cn.zwx.activiti.example.model.resp.InstancesByIdResp;
import cn.zwx.activiti.example.model.resp.InstancesByUserResp;
import cn.zwx.activiti.example.service.ActivitiHistoryService;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.runtime.api.query.impl.PageImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @projectName:activiti-example
 * @see:cn.zwx.activiti.example.service.impl
 * @author:zhangwenxue
 * @createTime:2021/6/8 20:26
 * @version:1.0
 */
@Service
public class ActivitiHistoryServiceImpl implements ActivitiHistoryService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private HistoryService historyService;

    /**
     * @description 查询用户历史任务
     * @return org.activiti.api.runtime.shared.query.Page<cn.zwx.activiti.example.model.resp.InstancesByUserResp>
     * @author zhangwenxue
     * @createTime 2021/6/8 20:31
     **/
    @Override
    public Page<InstancesByUserResp> getInstancesByUser(String userName, InstancesByUserReq req) {
        // 构建查询对象
        HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        // 计算条数
        long count = historicTaskInstanceQuery.orderByHistoricTaskInstanceEndTime().desc().taskAssignee(userName)
                .count();
        if (count<=0){
            return new PageImpl<>(new ArrayList<>(),Integer.valueOf(count+""));
        }
        // 查询接口集
        List<HistoricTaskInstance> list = historicTaskInstanceQuery.orderByHistoricTaskInstanceEndTime().desc().taskAssignee(userName)
                .listPage(req.getStartIndex(), req.getEndIndex());
        
        // 返回参数封装 
        List<InstancesByUserResp> returnList = CollectionUtil.newArrayList();
        for (HistoricTaskInstance taskInstance : list) {
            InstancesByUserResp resp = new InstancesByUserResp();
            resp.setTaskId(taskInstance.getId());
            resp.setTaskName(taskInstance.getName());
            resp.setDeleteReason(taskInstance.getDeleteReason());
            resp.setDurationInMillis(taskInstance.getDurationInMillis());
            resp.setStartTime(taskInstance.getStartTime());
            resp.setEndTime(taskInstance.getEndTime());
            resp.setProcessDefinitionId(taskInstance.getProcessDefinitionId());
            resp.setProcessInstanceId(taskInstance.getProcessInstanceId());
            returnList.add(resp);

        }
        return new PageImpl<>(returnList,returnList.size());
    }

    @Override
    public List<InstancesByIdResp> getInstancesById(String processInstanceId) {
        // 查询历史信息 
        List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceEndTime().asc()
                .processInstanceId(processInstanceId)
                .list();
        // 转换参数
        List<InstancesByIdResp> returnList = CollectionUtil.newArrayList();
        for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
            InstancesByIdResp resp = new InstancesByIdResp();
            resp.setTaskId(historicTaskInstance.getId());
            resp.setTaskName(historicTaskInstance.getName());
            resp.setAssignee(historicTaskInstance.getAssignee());
            resp.setDeleteReason(historicTaskInstance.getDeleteReason());
            resp.setClaimTime(historicTaskInstance.getClaimTime());
            resp.setDurationInMillis(historicTaskInstance.getDurationInMillis());
            resp.setTaskDefinitionKey(historicTaskInstance.getTaskDefinitionKey());
            resp.setStartTime(historicTaskInstance.getStartTime());
            resp.setEndTime(historicTaskInstance.getEndTime());
            resp.setProcessDefinitionId(historicTaskInstance.getProcessDefinitionId());
            resp.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
            returnList.add(resp);
        }
        // 返回参数 
        return returnList;
    }
}
